#include "abstractplot2d.h"
#include <QPainter>
#include <QMouseEvent>
#include "rotatedlabel.h"
#include "legendwidget.h"


AbstractPlot2D::AbstractPlot2D(QWidget *parent)
    : QWidget(parent)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setFocusPolicy(Qt::StrongFocus);
    setMouseTracking(true);
    rubberBandIsShown = false;

    zoomXAxis.append(Scale());
    zoomYAxis.append(Scale());
    curZoom = 0;

    QFont labelFont = font();
    labelFont.setPixelSize(18);
    labelFont.setFamily("LMRoman9");

    QPalette p = this->palette();
    p.setColor(QPalette::WindowText, Qt::black);

    _xLabel = new RotatedLabel(tr("X Axis"), this);
    _xLabel->setPalette(p);
    _xLabel->setBackgroundColor(Qt::white);
    _xLabel->setFont(labelFont);
    _xLabel->setAlignment(Qt::AlignCenter);

    _yLabel = new RotatedLabel(tr("Y Axis"), this);
    _yLabel->setPalette(p);
    _yLabel->setBackgroundColor(Qt::white);
    _yLabel->setFont(labelFont);
    _yLabel->setAlignment(Qt::AlignCenter);
    _yLabel->setAngle(-90.0);

    _title = new RotatedLabel(tr("Title"), this);
    _title->setPalette(p);
    _title->setBackgroundColor(Qt::white);
    _title->setFont(labelFont);
    _title->setAlignment(Qt::AlignCenter);

    _currentPointLabel = 0;
    _legendWidget = new LegendWidget(this);
    posLegend = QPoint(50, 100);
    _legendWidget->move(posLegend);
    legendIsMove = false;
}
//-----------------------------------------------------------------------------

QSize AbstractPlot2D::minimumSizeHint() const
{
    return QSize(300, 200);
}
//-----------------------------------------------------------------------------

QSize AbstractPlot2D::sizeHint() const
{
    return QSize(600, 400);
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::updateRubberBandRegion()
{
    QRect rect = rubberBandRect.normalized();
    update(rect.left(), rect.top(), rect.width(), 1);
    update(rect.left(), rect.top(), 1, rect.height());
    update(rect.left(), rect.bottom(), rect.width(), 1);
    update(rect.right(), rect.top(), 1, rect.height());
}
//-----------------------------------------------------------------------------

QRect AbstractPlot2D::plotRect(const QSize &viewPortSize)
{
    _title->adjustSize();
    _xLabel->adjustSize();
    _yLabel->adjustSize();

    int y = TopMargin;
    if (!_title->text().isEmpty())
        y += (_title->height() + LabelMargin);

    Scale scaleY = zoomYAxis.at(curZoom);
    int labelWidth = 0;
    foreach(double val, scaleY.vectorOfMajorTics()) {
        int lw = fontMetrics().width(QString::number(val));
        if (lw > labelWidth)
            labelWidth = lw;
    }
    if (labelWidth > MaxLabelWidth)
        labelWidth = MaxLabelWidth;

    int x = LeftMargin + LabelMargin + labelWidth + LabelMargin + SizeMajorTick;
    if (!_yLabel->text().isEmpty())
        x += _yLabel->width();

    int w = /*width()*/viewPortSize.width() - x - RightMargin;

    int labelHeight = fontMetrics().height();
    if (labelHeight > MaxLabelHeight)
        labelHeight = MaxLabelHeight;

    int h = /*height()*/viewPortSize.height() - y - SizeMajorTick - LabelMargin - labelHeight - LabelMargin - BottomMargin;
    if (!_xLabel->text().isEmpty())
        h -= _xLabel->height();

    return QRect(x, y, w, h);
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::drawSymbol(QPainter *painter, Symbol symbol, qreal size,
                                QPointF pos, const QColor &clr)
{
    painter->save();
    painter->setBrush(QBrush(clr));
    painter->setPen(clr);
    switch (symbol) {
    case Cross: {
            painter->drawLine(pos.x()-size, pos.y(), pos.x()+size, pos.y());
            painter->drawLine(pos.x(), pos.y()-size, pos.x(), pos.y()+size);
            break;
        }
    case Circle: {
            painter->drawEllipse(pos, size, size);
            break;
        }
    case Square: {
            painter->drawRect(pos.x()-size, pos.y()-size, 2*size, 2*size);
            break;
        }
    case TriangleUp: {
            QPolygonF polygon;
            polygon << QPointF(pos.x(), pos.y()-size)
                    << QPointF(pos.x()+size*0.866, pos.y()+size*0.5)
                    << QPointF(pos.x()-size*0.866, pos.y()+size*0.5);
            painter->drawPolygon(polygon);
            break;
        }
    case TriangleDown: {
            QPolygonF polygon;
            polygon << QPointF(pos.x(), pos.y()+size)
                    << QPointF(pos.x()+size*0.866, pos.y()-size*0.5)
                    << QPointF(pos.x()-size*0.866, pos.y()-size*0.5);
            painter->drawPolygon(polygon);
            break;
        }
    case Rhombus: {
            QPolygonF polygon;
            polygon << QPointF(pos.x(), pos.y()-size)
                    << QPointF(pos.x()+size, pos.y())
                    << QPointF(pos.x(), pos.y()+size)
                    << QPointF(pos.x()-size, pos.y());
            painter->drawPolygon(polygon);
            break;
        }
    default:
        break;
    }
    painter->restore();
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::drawPlot(QPainter *painter)
{
    painter->save();
    const double a11 = double(_plotRect.width())/_curScaleX.width();
    const double a12 = _plotRect.x() - _curScaleX.min() * a11;

    const double a21 = double(-_plotRect.height())/_curScaleY.width();
    const double a22 = _plotRect.y() - _curScaleY.max() * a21;

    // draw MajorTicks for Y Axis
    QPen tickPen(Qt::black);
    tickPen.setStyle(Qt::SolidLine);
    tickPen.setWidth(1);
    painter->setPen(tickPen);
    painter->setFont(font());

    int labelHeight = fontMetrics().height();
    if (labelHeight > MaxLabelHeight)
        labelHeight = MaxLabelHeight;

    foreach(double val, _curScaleY.vectorOfMajorTics()) {
        int Y = qRound(a21 * val + a22);
        painter->drawLine(_plotRect.x() - SizeMajorTick, Y, _plotRect.x(), Y);
        painter->drawText(0, Y - labelHeight/2,
                          _plotRect.x() - SizeMajorTick - LabelMargin, labelHeight,
                          Qt::AlignRight | Qt::AlignVCenter,
                          QString::number(val));
    }

    // draw MinorTicks for Y Axis
    if (_curScaleY.minorTicks() != 0) {
        foreach(double val, _curScaleY.vectorOfMinorTicks()) {
            int Y = qRound(a21 * val + a22);
            painter->drawLine(_plotRect.x() - SizeMinorTick, Y, _plotRect.x(), Y);
        }
    }

    // draw MajorTicks for X Axis
    foreach(double val, _curScaleX.vectorOfMajorTics()) {
        int X = qRound(a11 * val + a12);
        painter->drawLine(X, _plotRect.y() + _plotRect.height(), X,
                          _plotRect.y() + _plotRect.height() + SizeMajorTick);
        painter->drawText(X - MaxLabelWidth/2,
                          _plotRect.y() + _plotRect.height() + SizeMajorTick + LabelMargin,
                          MaxLabelWidth, labelHeight,
                          Qt::AlignHCenter | Qt::AlignTop,
                          QString::number(val));
    }

    // draw MinorTicks for X Axis
    if (_curScaleX.minorTicks() != 0) {
        foreach(double val, _curScaleX.vectorOfMinorTicks()) {
            int X = qRound(a11 * val + a12);
            painter->drawLine(X, _plotRect.y() + _plotRect.height(),
                              X, _plotRect.y() + _plotRect.height() + SizeMinorTick);
        }
    }

    // draw MinorGrid for Y Axis
    if (_gridY.showMinorGrid() && _curScaleY.minorTicks() > 0) {
        painter->setPen(_gridY.penOfMinorGrid());
        foreach(double val, _curScaleY.vectorOfMinorTicks()) {
            int Y = qRound(a21 * val + a22);
            painter->drawLine(_plotRect.x(), Y, _plotRect.x() + _plotRect.width(), Y);
        }
    }

    // draw MinorGrid for X Axis
    if (_gridX.showMinorGrid() && _curScaleX.minorTicks() > 0) {
        painter->setPen(_gridX.penOfMinorGrid());
        foreach(double val, _curScaleX.vectorOfMinorTicks()) {
            int X = qRound(a11 * val + a12);
            painter->drawLine(X, _plotRect.y(), X, _plotRect.y() + _plotRect.height());
        }
    }

    // draw MajorGrid for Y Axis
    if (_gridY.showMajorGrid()) {
        painter->setPen(_gridY.penOfMajorGrid());
        foreach(double val, _curScaleY.vectorOfMajorTics()) {
            int Y = qRound(a21 * val + a22);
            painter->drawLine(_plotRect.x(), Y, _plotRect.x() + _plotRect.width(), Y);
        }
    }

    // draw MajorGrid for X Axis
    if (_gridX.showMajorGrid()) {
        painter->setPen(_gridX.penOfMajorGrid());
        foreach(double val, _curScaleX.vectorOfMajorTics()) {
            int X = qRound(a11 * val + a12);
            painter->drawLine(X, _plotRect.y(), X, _plotRect.y() + _plotRect.height());
        }
    }

    painter->setPen(tickPen);
    painter->drawRect(_plotRect);

    // draw curves
    painter->setClipRect(_plotRect);
    QListIterator<bool> showListIt(_showCurvesList);
    QListIterator<CurveData> curvesListIt(_curvesList);
    while (curvesListIt.hasNext()) {
        const CurveData &cd = curvesListIt.next();
        const bool &show = showListIt.next();
        //const bool &show = showListIt.value();
        if (show) {
            const QVector<QPointF> &data = cd.data();
            QPolygonF polyline;

            foreach(QPointF p, data) {
                double x = a11 * p.x() + a12;
                double y = a21 * p.y() + a22;
                if ((p.x() >= _curScaleX.min()) &&
                    (p.x() <= _curScaleX.max()))
                    polyline << QPointF(x, y);
            }

            QPen curvePen;
            curvePen.setColor(cd.color());
            curvePen.setWidthF(cd.penWidth());
            curvePen.setStyle(cd.penStyle());

            painter->setPen(curvePen);
            painter->setRenderHint(QPainter::Antialiasing, true);
            painter->drawPolyline(polyline);
            foreach (QPointF p, polyline)
                drawSymbol(painter, cd.symbol(), cd.symbolSize(), p, cd.color());
            painter->setRenderHint(QPainter::Antialiasing, false);
        }
    }
    painter->restore();
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::refreshPixmap()
{
    pixmap = QPixmap(size());
    pixmap.fill(this, 0, 0);

    QPainter painter(&pixmap);
    painter.fillRect(rect(), Qt::white);
    _curScaleX = zoomXAxis[curZoom];
    _curScaleY = zoomYAxis[curZoom];
    _plotRect = plotRect(size());
    drawPlot(&painter);
    //draw labels
    _title->move((width()-_title->width())/2, TopMargin);
    _yLabel->move(LeftMargin, (height()-_yLabel->height())/2);
    _xLabel->move((width()-_xLabel->width())/2, height() - BottomMargin -_xLabel->height());
    update();
}
//-----------------------------------------------------------------------------

QPixmap AbstractPlot2D::render(const QSize &s)
{
    QPixmap pix(s);
    QPainter painter(&pix);
    painter.fillRect(0, 0, s.width(), s.height(), Qt::white);
    _curScaleX = zoomXAxis[curZoom];
    _curScaleY = zoomYAxis[curZoom];
    _plotRect = plotRect(s);
    drawPlot(&painter);
    //drawLabels
    QPixmap xAxisPix(_xLabel->size());
    _xLabel->render(&xAxisPix);
    painter.drawPixmap((s.width()-xAxisPix.width())/2,
                      s.height()-BottomMargin-xAxisPix.height(),
        xAxisPix.width(), xAxisPix.height(), xAxisPix);

    QPixmap yAxisPix(_yLabel->size());
    _yLabel->render(&yAxisPix);
    painter.drawPixmap(LeftMargin, (s.height()-yAxisPix.height())/2,
                       yAxisPix.width(), yAxisPix.height(), yAxisPix);

    QPixmap titlePix(_title->size());
    _title->render(&titlePix);
    painter.drawPixmap((s.width()-_title->width())/2, TopMargin,
                       titlePix.width(), titlePix.height(), titlePix);

    return pix;
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(0, 0, pixmap);

    if (rubberBandIsShown) {
        painter.setPen(Qt::gray);
        painter.drawRect(rubberBandRect.normalized()
                                       .adjusted(0, 0, -1, -1));
    }
    QWidget::paintEvent(event);
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::resizeEvent(QResizeEvent * /* event */)
{
    refreshPixmap();
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::mousePressEvent(QMouseEvent *event)
{

    if (_legendWidget->geometry().contains(event->pos())) {
        legendIsMove = true;
        setCursor(Qt::ClosedHandCursor);
        posOnLegend = event->pos() - _legendWidget->pos();
        return;
    }

    if (event->button() == Qt::LeftButton) {
        if (_plotRect.contains(event->pos()) && !_legendWidget->rect().contains(event->pos())) {
            rubberBandIsShown = true;
            rubberBandRect.setTopLeft(event->pos());
            rubberBandRect.setBottomRight(event->pos());
            updateRubberBandRegion();
        }
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setCurrentPointLabel(QLabel *label)
{
    _currentPointLabel = label;
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::mouseMoveEvent(QMouseEvent *event)
{
    if (legendIsMove) {
        QPoint p = event->pos()-posOnLegend;

        if (p.x() > 0 && p.x() < width() - _legendWidget->width())
            posLegend.rx() = p.x();

        if (p.y() > 0 && p.y() < height() - _legendWidget->height())
            posLegend.ry() = p.y();

        _legendWidget->move(posLegend);

        return;
    }

    if (_plotRect.contains(event->pos()) && !_legendWidget->geometry().contains(event->pos())) {
        setCursor(Qt::CrossCursor);

        if (_currentPointLabel) {
            double a11 = _curScaleX.width()/_plotRect.width();
            double a12 = _curScaleX.min() - a11 * _plotRect.x();
            double a21 = -_curScaleY.width()/_plotRect.height();
            double a22 = _curScaleY.max() - a21 * _plotRect.y();

            double x = a11 * event->x() + a12;
            double y = a21 * event->y() + a22;

            _currentPointLabel->setText(QString("x = %1,  y = %2").arg(x).arg(y));
        }

    } else {
        if (_currentPointLabel)
            _currentPointLabel->setText("");
        if (_legendWidget->geometry().contains(event->pos()))
           setCursor(Qt::CrossCursor);
        else
            unsetCursor();
    }

    if (rubberBandIsShown) {
        updateRubberBandRegion();
        rubberBandRect.setBottomRight(event->pos());
        updateRubberBandRegion();
    }

    if (_legendWidget->geometry().contains(event->pos())
        && !legendIsMove && !rubberBandIsShown) {
        setCursor(Qt::OpenHandCursor);
    }

}
//-----------------------------------------------------------------------------

void AbstractPlot2D::mouseReleaseEvent(QMouseEvent *event)
{
    if (legendIsMove) {
        legendIsMove = false;
        setCursor(Qt::OpenHandCursor);
        return;
    }

    if ((event->button() == Qt::LeftButton) && rubberBandIsShown) {
        rubberBandIsShown = false;
        updateRubberBandRegion();

        QRect rect = rubberBandRect.normalized();
        if (rect.width() < 4 || rect.height() < 4)
            return;

        rect.translate(-_plotRect.x(), -_plotRect.y());

        double dx = _curScaleX.width() / _plotRect.width();
        double dy = _curScaleY.width() / _plotRect.height();

        double minX = _curScaleX.min() + dx * rect.left();
        double maxX = _curScaleX.min() + dx * rect.right();
        double minY = _curScaleY.max() - dy * rect.bottom();
        double maxY = _curScaleY.max() - dy * rect.top();

        _curScaleX.setMin(minX);
        _curScaleX.setMax(maxX);
        _curScaleY.setMin(minY);
        _curScaleY.setMax(maxY);

        _curScaleX.adjust();
        _curScaleY.adjust();

        zoomXAxis.resize(curZoom + 1);
        zoomXAxis.append(_curScaleX);
        zoomYAxis.resize(curZoom + 1);
        zoomYAxis.append(_curScaleY);
        zoomIn();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {

    case Qt::Key_Left:
        scrollScale(zoomXAxis[curZoom], -1);
        refreshPixmap();
        break;
    case Qt::Key_Right:
        scrollScale(zoomXAxis[curZoom], 1);
        refreshPixmap();
        break;
    case Qt::Key_Down:
        scrollScale(zoomYAxis[curZoom], -1);
        refreshPixmap();
        break;
    case Qt::Key_Up:
        scrollScale(zoomYAxis[curZoom], 1);
        refreshPixmap();
        break;
    default:
        QWidget::keyPressEvent(event);
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::zoomIn()
{
    if (curZoom < zoomXAxis.count() - 1) {
        ++curZoom;
        refreshPixmap();
        emit zoomChanged(curZoom, zoomXAxis.count());
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::zoomOut()
{
    if (curZoom > 0) {
        --curZoom;
        refreshPixmap();
        emit zoomChanged(curZoom, zoomXAxis.count());
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setCurveData(const CurveData &curveData)
{
    _curvesList.append(curveData);
    _showCurvesList.append(true);
    refreshPixmap();
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setMinX(const double &minX)
{
    if (minX != _curScaleX.min()) {
        _curScaleX.setMin(minX);
        zoomXAxis[curZoom] = _curScaleX;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setMaxX(const double &maxX)
{
    if (maxX != _curScaleX.max()) {
        _curScaleX.setMax(maxX);
        zoomXAxis[curZoom] = _curScaleX;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setMinY(const double &minY)
{
    if (minY != _curScaleY.min()) {
        _curScaleY.setMin(minY);
        zoomYAxis[curZoom] = _curScaleY;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setMaxY(const double &maxY)
{
    if (maxY != _curScaleY.max()) {
        _curScaleY.setMax(maxY);
        zoomYAxis[curZoom] = _curScaleY;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setIncrementX(const double &increment)
{
    if (increment != _curScaleX.increment()) {
        _curScaleX.setIncrement(increment);
        zoomXAxis[curZoom] = _curScaleX;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setIncrementY(const double &increment)
{
    if (increment != _curScaleY.increment()) {
        _curScaleY.setIncrement(increment);
        zoomYAxis[curZoom] = _curScaleY;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setMinorTicksX(int minorTicks)
{
    if (minorTicks != _curScaleX.minorTicks()) {
        _curScaleX.setMinorTicks(minorTicks);
        zoomXAxis[curZoom] = _curScaleX;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setMinorTicksY(int minorTicks)
{
    if (minorTicks != _curScaleY.minorTicks()) {
        _curScaleY.setMinorTicks(minorTicks);
        zoomYAxis[curZoom] = _curScaleY;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setScaleX(const Scale &scaleX)
{
    if (_curScaleX != scaleX) {
        _curScaleX = scaleX;
        zoomXAxis[curZoom] = scaleX;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setScaleY(const Scale &scaleY)
{
    if (scaleY != _curScaleY) {
        _curScaleY = scaleY;
        zoomYAxis[curZoom] = scaleY;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setGridX(const Grid &grid)
{
    if (grid != _gridX) {
        _gridX = grid;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setGridY(const Grid &grid)
{
    if (grid != _gridY) {
        _gridY = grid;
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------


void AbstractPlot2D::removeCurve(int index)
{
    if (index >= 0 && index < _curvesList.size()) {
        _curvesList.removeAt(index);
        _showCurvesList.removeAt(index);
        refreshPixmap();
        emit removedCurve(index);
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::clearAllCurves()
{
    _curvesList.clear();
    _showCurvesList.clear();
    refreshPixmap();
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::showMajorGridX(bool show)
{
    if (show != _gridX.showMajorGrid()) {
        _gridX.showMajorGrid(show);
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::showMajorGridY(bool show)
{
    if (show != _gridY.showMajorGrid()) {
        _gridY.showMajorGrid(show);
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::showMinorGridX(bool show)
{
    if (show != _gridX.showMinorGrid()) {
        _gridX.showMinorGrid(show);
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::showMinorGridY(bool show)
{
    if (show != _gridY.showMinorGrid()) {
        _gridY.showMinorGrid(show);
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

Scale AbstractPlot2D::scaleX() const
{
    return zoomXAxis[curZoom];
}
//-----------------------------------------------------------------------------

Scale AbstractPlot2D::scaleY() const
{
    return zoomYAxis[curZoom];
}
//-----------------------------------------------------------------------------

Grid AbstractPlot2D::gridX() const
{
    return _gridX;
}
//-----------------------------------------------------------------------------

Grid AbstractPlot2D::gridY() const
{
    return _gridY;
}
//-----------------------------------------------------------------------------

CurveData AbstractPlot2D::curveData(int index) const
{
    if (index >= 0 && index < _curvesList.size()) {
        return _curvesList.at(index);
    }
    return CurveData();
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::showCurve(int index, bool show)
{
    if (index >= 0 && index < _showCurvesList.size()) {
        if (_showCurvesList.at(index) != show) {
            _showCurvesList.replace(index, show);
            refreshPixmap();
        }
    }
}
//-----------------------------------------------------------------------------

QList<CurveData> &AbstractPlot2D::curvesList()
{
    return _curvesList;
}
//-----------------------------------------------------------------------------

QList<bool> &AbstractPlot2D::showCurvesList()
{
    return _showCurvesList;
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setTitle(const QString &text)
{
    if (text != _title->text()) {
        _title->setText(text);
        if (text.isEmpty())
            _title->hide();
        else
            _title->show();

        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setNameXAxis(const QString &text)
{
    if (text != _xLabel->text()) {
        _xLabel->setText(text);
        if (text.isEmpty())
            _xLabel->hide();
        else
            _xLabel->show();

        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setNameYAxis(const QString &text)
{
    if (text != _yLabel->text()) {
        _yLabel->setText(text);
        if (text.isEmpty())
            _yLabel->hide();
        else
            _yLabel->show();

        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setTitleFont(const QFont &font)
{
    if (font != _title->font()) {
        _title->setFont(font);
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setXAxisFont(const QFont &font)
{
    if (font != _xLabel->font()) {
        _xLabel->setFont(font);
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setYAxisFont(const QFont &font)
{
    if (font != _yLabel->font()) {
        _yLabel->setFont(font);
        refreshPixmap();
    }
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setLegendFont(const QFont &font)
{
    _legendWidget->setFont(font);
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::setLegendOpacity(int opacity)
{
    _legendWidget->setOpacity(opacity);
}
//-----------------------------------------------------------------------------

QString AbstractPlot2D::title() const
{
    return _title->text();
}
//-----------------------------------------------------------------------------

QString AbstractPlot2D::nameXAxis() const
{
    return _xLabel->text();
}
//-----------------------------------------------------------------------------

QString AbstractPlot2D::nameYAxis() const
{
    return _yLabel->text();
}
//-----------------------------------------------------------------------------

QFont AbstractPlot2D::titleFont() const
{
    return _title->font();
}
//-----------------------------------------------------------------------------

QFont AbstractPlot2D::xAxisFont() const
{
    return _xLabel->font();
}
//-----------------------------------------------------------------------------

QFont AbstractPlot2D::yAxisFont() const
{
    return _yLabel->font();
}
//-----------------------------------------------------------------------------

QFont AbstractPlot2D::legendFont() const
{
    return _legendWidget->font();
}
//-----------------------------------------------------------------------------

int AbstractPlot2D::legendOpacity() const
{
    return _legendWidget->opacity();
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::updatePlot()
{
    _xLabel->updateLabel();
    _yLabel->updateLabel();
    _title->updateLabel();
    refreshPixmap();
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::showLegend(bool isShow)
{
    _legendWidget->setVisible(isShow);
}
//-----------------------------------------------------------------------------

void AbstractPlot2D::showLegendFrame(bool isShow)
{
    _legendWidget->showFrame(isShow);
}
//-----------------------------------------------------------------------------

bool AbstractPlot2D::legendFrameIsShow() const
{
    return _legendWidget->frameIsShow();
}
//-----------------------------------------------------------------------------
